﻿function Get-TSMAdminEvent {    
    <#
        .SYNOPSIS
            Get TSM admin events
                    
        .DESCRIPTION
            The Get-TSMAdminEvent returns recent admin events

        .EXAMPLE
            Get-TSMAdminEvent

        .NOTES
            Contributors:  David Seibel
            Last Revision Date: 09/03/2015

    #>
    [CmdletBinding()]
    Param(
        [Parameter(Position = 1)]
        $Schedule = "*",
        
        [Parameter(Position = 2)]
        [DateTime]$AfterDate = (Get-Date -Hour 0 -Minute 0 -Second 0),

        [Parameter(Position = 3)]
        [DateTime]$BeforeDate = (Get-Date -Hour 23 -Minute 59 -Second 59),
                
        [Parameter(Position = 4)]
        $Node
    )

    if ($AfterDate -lt $BeforeDate) {
        $whereProps = @()
        $whereProps += [PSCustomObject]@{ "Column" = "SCHEDULE_NAME";   "Operator" = "LIKE"; "Value" = $Schedule }
        $whereProps += [PSCustomObject]@{ "Column" = "SCHEDULED_START"; "Operator" = ">=";   "Value" = $AfterDate }
        $whereProps += [PSCustomObject]@{ "Column" = "SCHEDULED_START"; "Operator" = "<=";   "Value" = $BeforeDate }
        $whereProps += [PSCustomObject]@{ "Column" = "NODE_NAME";       "Operator" = "IS";   "Value" = "NULL" }

        $query = Format-TSMQuery -Table "EVENTS" -Where $whereProps -OrderBy "SCHEDULED_START"
        $result = Invoke-TSMCommand -Command $query

        $defaultDisplaySet = 'SCHEDULED_START','ACTUAL_START','SCHEDULE_NAME','STATUS'
        $typeDefinitions = @{
                             "ACTUAL_START"    = [DateTime]
                             "SCHEDULED_START" = [DateTime]
                             "COMPLETED"       = [DateTime]
                            }

        return Format-TSMData -Data $result -TypeDefinitions $typeDefinitions -DefaultDisplaySet $defaultDisplaySet
    } else {
        Write-Warning "Start date must be before the end date!"
    }
}